/**
 * 
 */
package com.mobile2you.nci.android;

import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mobile2you.nci.android.data.WebServicesRequest;
import com.mobile2you.nci.android.utils.Utils;

/**
 * @author JBromo
 * 
 */
public class MenuContact extends Activity {

	JSONObject object = new JSONObject();

	private ProgressDialog sendDialog;

	private class SendContactTask extends AsyncTask<String, String, Boolean> {

		@Override
		protected Boolean doInBackground(String... params) {
			return WebServicesRequest.sendContacts(object);
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			sendDialog.dismiss();
			if (result) {
				Toast.makeText(
						MenuContact.this,
						"You will be contacted by one of our representatives soon",
						Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(
						MenuContact.this,
						"Contact not sent: Application cannot communicate with the server !",
						Toast.LENGTH_LONG).show();
			}

		}

	}

	private void buildJSON(String name, String phone) {
		try {
			object.put("key", "fc68194ce2c81d3be065761c9292c4f5e9bade24");
			object.put("name", name);
			object.put("phone", phone);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private void initView() {

		final EditText nameEdit = (EditText) findViewById(R.id.edit_nom);
		final EditText phoneEdit = (EditText) findViewById(R.id.edit_phone);
		Button sendButton = (Button) findViewById(R.id.bt_contact_envoyer);
		final Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.editextanim);
		View numVertButton = findViewById(R.id.contact_bt_num_vert);
		numVertButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Utils.lanceCall(MenuContact.this, "0800488488");
			}
		});
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String name = nameEdit.getText().toString();
				String phone = phoneEdit.getText().toString();

				if (name == null || name.trim().length() < 3) {
					nameEdit.startAnimation(animation);
					Toast.makeText(MenuContact.this, "Invalide name",
							Toast.LENGTH_LONG).show();
				} else {
					if (phone == null || phone.trim().length() < 5) {
						phoneEdit.startAnimation(animation);
						Toast.makeText(MenuContact.this,
								"Invalide phone number", Toast.LENGTH_LONG)
								.show();
					} else {
						buildJSON(name, phone);
						showSendDialog();
					}
				}

			}
		});

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu_contact);
		initView();
	}

	private void showSendDialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("Envoi de contact ");
		builder.setMessage("Are you sure you want to send the contact information to the server ?");
		builder.setPositiveButton("Envoyer", new AlertDialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				sendDialog = ProgressDialog.show(MenuContact.this, "",
						"Envoie en cours");
				new SendContactTask().execute("");
			}
		});
		builder.setNegativeButton("Annuler", new AlertDialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();

			}

		});
		builder.create().show();
	}

}
